var mysql=require("mysql");

var crypto = require('./utils/crypto');

var pool = null;

function nop(a,b,c,d,e,f,g){
    
}
  
function query(sql,callback){  
    pool.getConnection(function(err,conn){  
        if(err){  
            callback(err,null,null);  
        }else{  
            conn.query(sql,function(qerr,vals,fields){  
                //释放连接  
                conn.release();  
                //事件驱动回调  
                callback(qerr,vals,fields);  
            });  
        }  
    });  
};

function checkCallback(callback) {
	return callback = callback == null? nop: callback;
}

exports.init = function(config){
    pool = mysql.createPool({  
        host: config.HOST,
        user: config.USER,
        password: config.PSWD,
        database: config.DB,
        port: config.PORT,
    });
};

/*
 *
 * 登陆操作 table：t_accounts <账号、密码>
 *
*/

//添加一个账号
exports.create_account = function(account, password, callback) {
	callback = checkCallback(callback)
	if (!account || !password) {
		callback(false);
		return;
	}	
	var pwd = crypto.md5(password);
	var sql = 'INSERT INTO t_accounts (account, password) VALUES ("' + account + '","' + pwd + '")';
	//sql = sql.format(account, pwd);
	console.log("[ create_account = > ", sql);
	query(sql, function(err, rows, fields) {
		if (err) {
			callback(false);
			throw err;
		}
		callback(true);
	})
}

//登陆验证
exports.is_account_password = function(account, password, callback) {
	callback = checkCallback(callback);
	if (account == null || password == null) {
		callback(false);  
		return ;
	}
	var pwd = crypto.md5(password);
	var sql = 'SELECT * FROM t_accounts WHERE account = "'+ account + '" AND password = "' + pwd + '"';
	//sql = sql.format(account, pwd);
	console.log("[ is_account_password = > ", sql);
	query(sql, function(err, rows, fields) {
		if (err) {
			callback(false);
			throw err;
		}
		if (rows.length == 0) {
			callback(false);
			return ;
		}
		callback(true);
	})
}

//检测账号存不存在<注册的时候检测>
exports.is_esxit_account = function(account, callback) {
	callback = checkCallback(callback);
	if (account == null) {
		return ;
	}
	var sql = 'SELECT account FROM t_accounts WHERE account = "' + account + '"';
	console.log("[ is_esxit_account = > ", sql);
	//sql = sql.format(account);
	query(sql, function(err, rows, fields) {
		if (err) {
			throw err;
		}
		if (rows.length == 0) {
			callback(false);
			return;
		}
		callback(true);
	})
}


/*
 *
 * 大厅操作 table：t_accounts <账号、密码>
 *
*/


/*
 *
 * 房间操作 table：t_accounts <账号、密码>
 *
*/
exports.get_gems = function(account, callback) {
	callback = checkCallback(callback);
	if (account == null) {
		callback(null);
		return;
	}
	var sql = 'SELECT gems FROM t_users WHERE account = "{0}"'
	sql = sql.foramt({account});
	console.log('sql=>' + sql);
	query(sql, function(err, rows, fields) {
		if(err) {
			callback();
			throw err;
		}
		if (rows.length == 0) {
			callback();
			return;
		}
		callback(rows[0]);
	})
}

exports.set_room_id_of_user = function(userId,roomId,callback) {
	callback = checkCallback(callback);
	if (userId == null || roomId == null) {
		callback();
	}
	var sql = 'UPDATE t_users SET roomid = "{0}"WHERE userid "{1}"';
	sql = sql.format({roomId, userId});
	console.log(sql);
	query(sql, function(err, rows, fields) {
		if (err) {
			callback(false);
			throw err;
		} else {
			callback(rows.length > 0);
		}
	});
}

exports.get_room_addr = function(roomId, callback) {
	callback = checkCallback(callback);
	if (roomId == nul) {
		callback(false);
	}
	var sql = 'SELECT ip,port FROM t_rooms WHERE id = "{0}"';
	sql = sql.format({roomId});
	query(sql, function(err, rows, fields) {
		if (err) {
			callback(false);
			throw err;
		}
		if (rows.length > 0) {
			callback(true, rows[0].ip, rows[0].port);
		} else {
			callback(false);
		}
	});
}
